home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $defint.P < prev    next >
Text File  |  1990-04-12  |  5KB  |  143 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $defint.P */
  25.  
  26. $defint_export([$defint_call/4]).
  27.  
  28. /* $defint_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
  29.     $functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$floor/2]).
  30.    $defint_use($buff,
  31.     [$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,$symtype/2,
  32.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  33.         $pred_undefined/1, $hashval/3]).
  34. */
  35.  
  36. $defint_call(Predterm,Arity,Genclfact,Introutine) :- 
  37.     $arity(Genclfact,Arity1),
  38.     (Arity=:=Arity1,
  39.         $defint_ercept(Predterm,Arity,Genclfact,Introutine)
  40.     ;
  41.      Arity=\=Arity1,
  42.         $defint_erpret(Predterm,Arity,Genclfact,Introutine)
  43.     ).
  44.  
  45. $defint_erpret(Predterm,Arity,Genclfact,'_$interp'(_,_)) :- !,
  46.     /* for Predterm = p(X,Y) , 
  47.        and Genclfact = cl$p(_,_,_),
  48.        generate code for:
  49.         p(A1,A2) :- _$savecp(CP),cl$p(A1,A2,B),_$interp(B,CP).
  50.     */
  51.     Arity1 is Arity+1,    /* no-op if not! */
  52.     $arity(Genclfact,Arity1),
  53.     $alloc_perm(56,Buff),
  54.         $opcode( allocate,   Op1 ),
  55.     $opcode( getpbreg,   Op2 ),
  56.     $opcode( putpvar,    Op3 ),
  57.     $opcode( call,       Op4 ),
  58.     $opcode( putuval,    Op5 ),
  59.     $opcode( deallocate, Op6 ),
  60.     $opcode( execute,    Op7 ),
  61.     $buff_code(Buff,0,14 /*ptv*/ ,Buff), /* set back-ptr */
  62.     $defint_code_list([Op1,0,        /* allocate        */
  63.                Op2,2,         /* getpbreg(2)        */
  64.                Op3,0,3,Arity1,    /* putpvar(3,Arity+1)    */
  65.                Op4,4         /* call cl$??,4        */
  66.               ],Buff,4),
  67.     $buff_code(Buff,24,0 /*ppsc*/ ,Genclfact),
  68.     $defint_code_list([Op5,0,3,1,        /* putuval(3,1)        */
  69.                Op5,0,2,2,        /* putuval(2,2)        */
  70.                Op6,0,        /* deallocate        */
  71.                Op7,0        /* execute(_$interp)    */
  72.               ],Buff,28),
  73.     $buff_code(Buff,52,0 /*ppsc*/ ,'_$interp'(_,_)),
  74.     $buff_code(Predterm,0,9 /*pep*/ ,Buff).
  75.  
  76. $defint_erpret(Predterm,Arity,Genclfact,'_$call'(_)) :- !,
  77.     /* for Predterm = p(X,Y) , 
  78.        and Genclfact = cl$p(_,_,_),
  79.        generate code for:
  80.         p(A1,A2) :- cl$p(A1,A2,B),_$call(B).
  81.     */
  82.     $arity(Genclfact,Arity1),
  83.     Arity1 is Arity+1, /* no-op if not! */
  84.     $alloc_perm(44,Buff),
  85.     $opcode( allocate,   Op1 ),
  86.     $opcode( putpvar,    Op2 ),
  87.     $opcode( call,       Op3 ),
  88.     $opcode( putuval,    Op4 ),
  89.     $opcode( deallocate, Op5 ),
  90.     $opcode( execute,    Op6 ),
  91.     $buff_code(Buff,0,14 /*ptv*/ ,Buff), /* set back-ptr */
  92.     $defint_code_list([Op1,0,        /* allocate        */
  93.                Op2,0,2,Arity1,    /* putpvar(2,Arity+1)    */
  94.                Op3,3         /* call cl$??,3        */
  95.               ],Buff,4),
  96.     $buff_code(Buff,20,0 /*ppsc*/ ,Genclfact),
  97.     $defint_code_list([Op4,0,2,1,        /* putuval(2,1)        */
  98.                Op5,0,        /* deallocate        */
  99.                Op6,0        /* execute(_$call)    */
  100.               ],Buff,24),
  101.     $buff_code(Buff,40,0 /*ppsc*/ ,'_$call'(_)),
  102.     $buff_code(Predterm,0,9 /*pep*/ ,Buff).
  103.  
  104. $defint_erpret(Predterm,Arity,Genclfact,Interprout) :- 
  105.     /* for Predterm = p(X,Y) , 
  106.        and Genclfact = cl$p(_,_,_),
  107.        and Interprout = _$interpgoal(_,_)
  108.        generate instruction: unexeci cl$p,_$interpgoal
  109.     */
  110.     $arity(Genclfact,Arity1),
  111.     Arity1 is Arity+1, /* fail if not! */
  112.     $alloc_perm(16,Buff),
  113.     $buff_code(Buff,0,14 /*ptv*/ ,Buff), /* set back-ptr */
  114.         $opcode( unexeci, UnxiOp ),
  115.     $buff_code(Buff, 4, 3 /*ps*/ , UnxiOp ),
  116.     $buff_code(Buff, 6, 3 /*ps*/ , 0 ),
  117.     $buff_code(Buff, 8, 0 /*ppsc*/ ,Genclfact),
  118.     $buff_code(Buff,12, 0 /*ppsc*/ ,Interprout),
  119.     $buff_code(Predterm,0,9 /*pep*/ ,Buff).
  120.  
  121. $defint_ercept(Predterm,Arity,Genclfact,Interprout) :-
  122.     /* for Predterm = p(X,Y) , 
  123.        and Genclfact = code$p(_,_),
  124.        and Interprout = _$traceintercept(_)
  125.        defines p(X,Y) :- _$traceintercept(code$p(X,Y)) by
  126.             generating instruction: unexec code$p,_$traceintercept
  127.     */
  128.     $arity(Genclfact,Arity),    /* error if not same arity */
  129.     $alloc_perm(16,Buff),
  130.     $buff_code(Buff,0,14 /*ptv*/ ,Buff), /* set back-ptr */
  131.         $opcode( unexec, UnxOp ),
  132.     $buff_code(Buff, 4, 3 /*ps*/ , UnxOp ),
  133.     $buff_code(Buff, 6, 3 /*ps*/ , 0 ),
  134.     $buff_code(Buff, 8, 0 /*ppsc*/ ,Genclfact),
  135.     $buff_code(Buff,12, 0 /*ppsc*/ ,Interprout),
  136.     $buff_code(Predterm,0,9 /*pep*/ ,Buff).
  137.  
  138. $defint_code_list([],_,_).
  139. $defint_code_list([Byte|Rest],Buff,Loc) :-
  140.     $buff_code(Buff,Loc,3 /*ps*/ ,Byte),
  141.     Nextloc is Loc+2,
  142.     $defint_code_list(Rest,Buff,Nextloc).
  143.